<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <script type="text/javascript">
      var description                                                           //因为使用了FCKeditor，除了extjs本身管理，还需要额外赋值，要使用全局变量
      Ext.onReady(function(){
        var trainTaskId = <%= params[:trainTaskId]%>

        var trainTaskSnText = new Ext.form.TextField({
          fieldLabel:'检修任务编号',
          allowBlank:true,
          anchor: '90%',
          name:'sn'
        })

        var deviceIDText = new Ext.form.TextField({
          id:'device_id',
          fieldLabel:'设备ID',
          allowBlank:false,
          readOnly:true,
          anchor: '90%',
          blankText:'设备ID不能为空！'
        })

        var trackDS = new Ext.data.Store({
          proxy:new Ext.data.HttpProxy({url:'/track/getAllTracks?for_cbb=true'}),
          autoLoad:true,
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'track'
          }, [
            {name:'track_name'},
            {name:'device_id'},
            {name:'id'}
          ])
        });
        var stopTrack = new Ext.form.ComboBox({  //配合上面的departmentDS，完成动态生成的下拉框
          store: trackDS,
          hiddenName:'stop_track',            //显示时：这里写后台传送过来的字段，一旦和valueField相等，就进行显示。不相等为空。
          displayField:'track_name',              //store里面的值，用于显示在表面上的值
          valueField:'track_name',                  //store里面的值，和display联动
          typeAhead: true,
          allowBlank: false,
          minChars:1,
          readOnly:false,
          anchor: '90%',                    //在容器中的时候，自动按照容器百分比进行调整，会覆盖宽度的属性
          mode: 'remote',
          fieldLabel:'股道选择',
          forceSelection: true,
          triggerAction: 'all',
          emptyText:'请选择股道.....',
          selectOnFocus:true,
          listeners:{
            select:function(combo, record, index){
              deviceIDText.setValue(record.get('device_id'))
            }
          }
        });
        
        //时间选择模块
        var DeadLineDateTime = new Ext.ux.form.DateTimeField({
          fieldLabel: '计划检修开工时间',
          anchor: '90%',                    //在容器中的时候，自动按照容器百分比进行调整，会覆盖宽度的属性
          name: 'begin_time',
          id: 'begin_time',
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: false,
          blankText: "完工时间必须选择",
          emptyText: '请选择时间...'
        });

        var trainDS = new Ext.data.Store({
          proxy:new Ext.data.HttpProxy({url:'/train/getTrainsForCombo?for_cbb=true'}),
          autoLoad:true,
          reader:new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'train'
          }, [
            {name:'sn'},                            //用作后面的displayField:
            {name:'id'}                                //用作后面的valueField:
          ])
        });
        var trainIdCombo = new Ext.form.ComboBox({  //配合上面的departmentDS，完成动态生成的下拉框
          store: trainDS,
          hiddenName:'train_id',            //显示时：这里写后台传送过来的字段，一旦和valueField相等，就进行显示。不相等为空。
          displayField:'sn',              //store里面的值，用于显示在表面上的值
          valueField:'id',                  //store里面的值，和display联动
          typeAhead: true,
          allowBlank:false,
          minChars:1,
          readOnly:false,
          anchor: '90%',                    //在容器中的时候，自动按照容器百分比进行调整，会覆盖宽度的属性
          mode: 'remote',
          fieldLabel:'列车选择',
          forceSelection: true,
          triggerAction: 'all',
          emptyText:'请选择列车.....',
          selectOnFocus:true
        });

        var descriptionHtmlEditor = new Ext.form.HtmlEditor({
          fieldLabel: '内容',
          name: 'description',
          width:755,
          height:325
        })


        var editTrainTaskForm = new Ext.form.FormPanel({
          width:780,
          labelAlign: 'top',
          url:'/traintask/update',
          buttonAlign:'center',
          bodyStyle: 'padding:5px',
          labelWidth:100,
          frame: true,
          reader:new Ext.data.JsonReader({
          },[
            {name:'id'},
            {name:'sn'},
            {name:'description'},
            {name:'begin_time'},
            {name:'train_id'},
            {name:'stop_track'},
            {name:'device_id'}
          ]),
          items:[{
              layout:'column',
              items:[{
                  columnWidth:.3,
                  layout:'form',
                  items:[trainIdCombo]
                },{
                  columnWidth:.3,
                  layout:'form',
                  items:[stopTrack]
                },{
                  columnWidth:.3,
                  layout:'form',
                  items:[deviceIDText]
                }
              ]
            },
            {
              xtype: 'hidden',
              name:  'id',
              value: trainTaskId
            }
            ,{
              layout:'column',
              items:[{
                  columnWidth:.3,
                  layout:'form',
                  items:[trainTaskSnText]
                },{
                  columnWidth:.3,
                  layout:'form',
                  items:[DeadLineDateTime]
                }
              ]
            },descriptionHtmlEditor
          ],
          buttons:[{
              text:'确认修改',
              handler: function(){
                if (editTrainTaskForm.form.isValid()) {
                  editTrainTaskForm.form.submit({
                    method: 'post',
                    success: function(result, response){
                      if (response.result.info == "success") {
                        editTrainTaskWin.destroy();
                      }
                      else {
                        Ext.MessageBox.alert('消息', response.result.info);
                      }
                    },
                    failure: function(result, response){
                      Ext.MessageBox.alert('提示', result.responseText);
                    }
                  });
                }
                else {
                  Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                }
              }
            },{
              text:'取消',
              handler:function(){
                editTrainTaskWin.destroy();
              }
            }]
        });

        

        trainDS.load({
          callback:function(r,options,success){
            if(success){
              editTrainTaskForm.form.load({
                url: '/traintask/edit?id=' + trainTaskId,
                waitMsg : '正在载入数据...',
                success: function(form, action){
                  var beginTime = action.result.data.begin_time;                      //load的时候，取得的值，由此语法即可使用，这里取出了deadline
                  beginTime = beginTime.substr(0,19);
                  beginTime = beginTime.replace(/\//g,"-")
                  DeadLineDateTime.setValue(beginTime);
                },
                failure: function(form, action){
                  Ext.MessageBox.alert('提示信息', '信息加载失败');
                }
              });
            }else{
              Ext.MessageBox.alert('提示信息', '信息加载失败');
            }
          }
        })

        editTrainTaskForm.render('editTrainTaskForm');
      })


    </script>
  </head>
  <body>
    <div id="editTrainTaskForm"></div>
  </body>
</html>